package com.alibaba.citrus.cr.mn.order.center.forecastorder.convert;

import com.alibaba.citrus.cr.common.enums.ordercenter.ForecastOrderTypeEnum;
import com.alibaba.citrus.cr.common.enums.ordercenter.OrderCategoryEnum;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.cz.base.tool.log.Log;
import com.epoch.app.bcorder.platform_enhance.SaleOrderTag;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import com.epoch.app.mnordercenter.model.dto.QueryForecastResponse;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * @author zhangxiaorui
 * @ClassName AtomOrderDOToSalesOrderAtomQueryResponseConvertor
 * @description: TODO
 * @date 2023/04/27 13:28
 * @version: 1.0
 */
public class ForecastOrderSDOToResponseConvertor {

    private static final Log log = Log.getLogger(ForecastOrderSDOToResponseConvertor.class);

    public static QueryForecastResponse convert(ForecastOrderSDO forecastOrderSDO) {
        QueryForecastResponse response = new QueryForecastResponse();
        response.setSourceOrderNo(nvlToEmptyStr(forecastOrderSDO.getSourceOrderNo()));
        response.setSalesOrderType(nvlToEmptyStr(forecastOrderSDO.getOrderType()));
        response.setSalesOrderTypeName(nvlToEmptyStr(""));
        if(StringUtils.isNotBlank(forecastOrderSDO.getOrderType())&& SaleOrderTag.of(forecastOrderSDO.getOrderType())!=null) {
            response.setSalesOrderTypeName(nvlToEmptyStr(SaleOrderTag.of(forecastOrderSDO.getOrderType()).getDesc()));
        }
        response.setSalesOrderNo(nvlToEmptyStr(forecastOrderSDO.getOrderBizId()));
        response.setDeliveryMethodCode(nvlToEmptyStr(forecastOrderSDO.getTransportModeCode()));
        response.setDeliveryMethodName(nvlToEmptyStr(forecastOrderSDO.getTransportModeName()));
        response.setExpectedDeliveryDate(nvlToEmptyStr(formatDate(forecastOrderSDO.getCustomerExpectTDate(), DateUtils.DATE_FORMAT)));
        response.setShipToPartyCode(nvlToEmptyStr(forecastOrderSDO.getShipToPartyCode()));
        response.setShipToPartyName(nvlToEmptyStr(forecastOrderSDO.getShipToPartyName()));
        response.setCustomerCode(nvlToEmptyStr(forecastOrderSDO.getCustomerCode()));
        response.setCustomerName(nvlToEmptyStr(forecastOrderSDO.getCustomerName()));
        response.setProductGroupCode(nvlToEmptyStr(forecastOrderSDO.getProductGroupCode()));
        response.setProductGroupName(nvlToEmptyStr(forecastOrderSDO.getProductGroupName()));
        response.setBusinessChannel(nvlToEmptyStr(forecastOrderSDO.getChannelCode()));
        response.setBusinessChannelName(nvlToEmptyStr(forecastOrderSDO.getChannelName()));
        response.setOrderReasonCode(nvlToEmptyStr(forecastOrderSDO.getCreateReason()));
        response.setOrderReasonName(nvlToEmptyStr(forecastOrderSDO.getCreateReasonName()));
        response.setShippingWarehouseCode(nvlToEmptyStr(forecastOrderSDO.getShippingWarehouseCode()));
        response.setShippingWarehouseName(nvlToEmptyStr(forecastOrderSDO.getShipAddressInfo()));
        response.setDeliveryFactoryCode(nvlToEmptyStr(forecastOrderSDO.getDeliveryFactoryCode()));
        response.setStockCode(nvlToEmptyStr(forecastOrderSDO.getStockCode()));
        response.setCustomerPurchaseOrderNo(nvlToEmptyStr(forecastOrderSDO.getCustomerPurchaseOrderNo()));
        response.setCustomerPurchaseDate("");
        if (forecastOrderSDO.getCustomerPurchaseDate() != null) {
            response.setCustomerPurchaseDate(DateUtils.toDateStr(forecastOrderSDO.getCustomerPurchaseDate(), DateUtils.DATE_FORMAT));
        }
        response.setNote(nvlToEmptyStr(forecastOrderSDO.getRemark()));
        response.setSalesOrganizationCode(nvlToEmptyStr(forecastOrderSDO.getSalesOrganizationCode()));
        response.setSalesOrganizationName(nvlToEmptyStr(forecastOrderSDO.getOrganizationName()));
        response.setCreatorCode(nvlToEmptyStr(forecastOrderSDO.getCreatorCode()));
        response.setCreatorName(nvlToEmptyStr(forecastOrderSDO.getCreatorName()));
        response.setCreateTime(nvlToEmptyStr(formatDate(forecastOrderSDO.getGmtCreate(),DateUtils.DATETIME_FORMAT)));
        response.setModifierCode(nvlToEmptyStr(forecastOrderSDO.getModifierCode()));
        response.setModifierName(nvlToEmptyStr(forecastOrderSDO.getModifierName()));
        response.setModifierTime(nvlToEmptyStr(formatDate(forecastOrderSDO.getGmtModified(),DateUtils.DATETIME_FORMAT)));
        response.setBusinessType(nvlToEmptyStr(Optional.ofNullable(forecastOrderSDO.getBusinessType()).orElse("")));
        response.setFromSys(nvlToEmptyStr(forecastOrderSDO.getFromSys()));
        response.setBusinessUnitCode(nvlToEmptyStr(forecastOrderSDO.getBusinessUnitCode()));
        response.setBusinessUnitName(nvlToEmptyStr(forecastOrderSDO.getBusinessUnitName()));
        response.setOrderStatus(forecastOrderSDO.getStatus());
        response.setBizFeatures(nvlToEmptyStr(forecastOrderSDO.getBizFeatures()));
        response.setOuterOrderCode(nvlToEmptyStr(forecastOrderSDO.getOuterOrderId()));
        response.setOrderCategoryCode(nvlToEmptyStr(forecastOrderSDO.getOrderCategoryCode()));
        response.setOrderCategoryName("");
        OrderCategoryEnum orderCategoryEnum = OrderCategoryEnum.of(forecastOrderSDO.getOrderCategoryCode());
        if(orderCategoryEnum != null) {
            response.setOrderCategoryName(orderCategoryEnum.getDesc());
        }
        response.setOrderChannelCode(nvlToEmptyStr(forecastOrderSDO.getOrderChannelCode()));
        response.setSalesOrderType(nvlToEmptyStr(forecastOrderSDO.getOrderTypeCode()));
        response.setSalesOrderTypeName("");
        ForecastOrderTypeEnum forecastOrderTypeEnum = ForecastOrderTypeEnum.of(forecastOrderSDO.getOrderTypeCode());
        if(forecastOrderTypeEnum != null) {
            response.setSalesOrderTypeName(forecastOrderTypeEnum.getDesc());
        }
        response.setShippingWarehouseCode(nvlToEmptyStr(forecastOrderSDO.getShippingWarehouseCode()));
        response.setShippingWarehouseName(nvlToEmptyStr(forecastOrderSDO.getShippingWarehouseName()));
        response.setSalesChannelCode(nvlToEmptyStr(forecastOrderSDO.getSaleChannelCode()));
        response.setSalesChannelName(nvlToEmptyStr(forecastOrderSDO.getSaleChannelName()));
        response.setSalesDepartmentCode(nvlToEmptyStr(forecastOrderSDO.getSalesDepartmentCode()));
        response.setSalesDepartmentName(nvlToEmptyStr(forecastOrderSDO.getSalesDepartmentName()));
        response.setAutoFulfillment(nvlToEmptyStr(forecastOrderSDO.getAutoFulfillment()));
        response.setFulfillmentTypeCode(nvlToEmptyStr(forecastOrderSDO.getFulfillmentTypeCode()));
        response.setFulfillmentPriority(nvlToEmptyStr(forecastOrderSDO.getFulfillmentPriority()));
        response.setKaSystem(nvlToEmptyStr(forecastOrderSDO.getKaSystem()));
        response.setKaArea(nvlToEmptyStr(forecastOrderSDO.getKaArea()));
        response.setOrderBizTag(forecastOrderSDO.getOrderBizTag());
        //response.setVaildBeginDate(formatDate(forecastOrderSDO.getVaildBeginTime(),DateUtils.DATE_FORMAT));
        response.setSourceOrderTime("");
        if (Objects.nonNull(forecastOrderSDO.getSourceOrderDate())){
            response.setSourceOrderTime(DateUtils.toDateStr(forecastOrderSDO.getSourceOrderDate(),DateUtils.DATETIME_FORMAT));
        }
        response.setVaildBeginDate("");
        if (Objects.nonNull(forecastOrderSDO.getVaildBeginTime())){
            response.setVaildBeginDate(DateUtils.toDateStr(forecastOrderSDO.getVaildBeginTime(),DateUtils.DATE_FORMAT));
        }
        response.setCustomerGrade(nvlToEmptyStr(forecastOrderSDO.getCustomerGrade()));
        response.setCustomerGroup(nvlToEmptyStr(forecastOrderSDO.getCustomerGroup()));
        response.setCustomerGroupName(nvlToEmptyStr(forecastOrderSDO.getCustomerGroupName()));
        return response;
    }

    private static String formatDate(Date dte, Object... pattern) {
        if (Objects.nonNull(dte)) {
            return DateUtils.formatDate(dte, pattern);
        }
        return null;
    }

    /**
     * 字段值如果为空，返回空字符串，而不是null。
     */
    private static String nvlToEmptyStr(String source) {
        if (StringUtils.isBlank(source)) {
            return "";
        }
        return source;
    }
}
